package com.example.ceramics.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ceramics.R;
import com.example.ceramics.database.dao.IOrderDao;
import com.example.ceramics.database.dao.OrderDaoImpl;
import com.example.ceramics.entity.GoodsEntity;
import com.example.ceramics.entity.OrderEntity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class OrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<OrderEntity> datas;

    private OnItemClickListener mOnItemClickListener;

    private IOrderDao orderDao;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public List<OrderEntity> getDatas() {
        return datas;
    }

    public void setDatas(List<OrderEntity> datas) {
        this.datas = datas;
    }

    public OrderAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public OrderAdapter(Context mContext, List<OrderEntity> datas) {
        this.mContext = mContext;
        this.datas = datas;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_order_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    //给控件对象赋值，给ViewHoleder绑定数据
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        orderDao = new OrderDaoImpl(mContext);
        ViewHolder viewHolder = (ViewHolder) holder;
        OrderEntity order = datas.get(position);
        GoodsEntity goodsEntity = order.getGoodsEntity();
        viewHolder.tvName.setText(goodsEntity.getGoodsName());
        viewHolder.tvPrice.setText("￥" + order.getPrice());
        viewHolder.tvTime.setText(order.getTime());
        viewHolder.ivImg.setImageResource(goodsEntity.getGoodsImg());
        viewHolder.tvNum.setText("x"+order.getNum());

        viewHolder.tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(goodsEntity);
            }
        });
        viewHolder.tvPrice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(goodsEntity);
            }
        });
        viewHolder.tvTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(goodsEntity);
            }
        });
        viewHolder.ivImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(goodsEntity);
            }
        });

        viewHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (datas.contains(order)) {
                    datas.remove(order);
                    notifyDataSetChanged();
                    orderDao.deleteOrder(order.getId());
                    Toast.makeText(mContext, "删除成功！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (datas != null && datas.size() > 0) {
            return datas.size();
        }
        return 0;
    }

    //绑定控件，得到控件对象
    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tvName;
        private TextView tvPrice;
        private TextView tvTime;
        private ImageView ivImg;
        private ImageView ivDelete;
        private TextView tvNum;

        public ViewHolder(@NonNull View view) {
            super(view);
            tvName = view.findViewById(R.id.tv_name);
            tvPrice = view.findViewById(R.id.tv_price);
            tvTime = view.findViewById(R.id.tv_time);
            ivImg = view.findViewById(R.id.iv_img);
            ivDelete = view.findViewById(R.id.iv_delete);
            tvNum = view.findViewById(R.id.tv_num);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(Serializable obj);
    }
}
